package cn.com.dcsgo.settlement.calculator;

import cn.com.dcsgo.settlement.OrderAmountContext;
import cn.com.dcsgo.settlement.model.Commodity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品总金额计算器
 *
 * @author Dcsgo
 * @since 2025/11/17 18:26
 */
@Component
public class CommoditiesAmountCalculator extends AbstractOrderAmountCalculator {
    @Override
    public void calculate(OrderAmountContext orderAmountContext) {
        List<Commodity> commodities = orderAmountContext.getCommodities();
        BigDecimal totalMoney = BigDecimal.ZERO;
        for (Commodity commodity : commodities) {
            totalMoney = totalMoney.add(commodity.getPrice().multiply(BigDecimal.valueOf(commodity.getCount())));
        }
        orderAmountContext.setCommoditiesAmount(totalMoney);
        if (getNextCalculator() != null) {
            getNextCalculator().calculate(orderAmountContext);
        }
    }
}
